
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-28 下午2:28:48 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.label.ErrMatcher;
import org.alipay4j.model.label.Matcher;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: 用户标签操作组件 
 * @Description: 用户标签操作组件 
 * @author heliang
 * @date 2019-8-28 下午2:28:48 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */

public class UserLabelComponent extends AbstractComponent  {

    public UserLabelComponent(Alipay alipay) {
        super(alipay);
    }

    /**
     * 用户取消标签接口 
     * @param userIds 用户userIds，最多传入10条
     * @param label_id 标签id
     * @return List<ErrMatcher>
     * @throws AlipayException 
     */
    public List<ErrMatcher> cancel(String[] userIds,String label_id) throws AlipayException {
        List<Matcher> matchers = new ArrayList<Matcher>();
        for(String userId : userIds) {
            Matcher matcher = new Matcher(userId);
            matchers.add(matcher);
        }
        return cancel(matchers, label_id);
    }
    
    /**
     * 用户取消标签接口 
     * @param matchers 支付宝用户匹配器列表，最多传入10条
     * @param label_id 标签id
     * @return List<ErrMatcher>
     * @throws AlipayException 
     */
    public List<ErrMatcher> cancel(List<Matcher> matchers,String label_id) throws AlipayException {
        if (ObjectHelper.isEmpty(label_id)) {
            throw new IllegalArgumentException("label_id can't be null or empty");
        }
        if (ObjectHelper.isEmpty(matchers)) {
            throw new IllegalArgumentException("matchers can't be null or empty");
        }
        if(matchers.size()>10) {
            throw new IllegalArgumentException("matchers 最多传入10条");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("label_id", label_id);
        body.put("matchers", JSONObject.toJSONString(matchers));
        body.put("method", "alipay.open.public.matchuser.label.delete");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_matchuser_label_delete_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_matchuser_label_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<ErrMatcher> rsts = new ArrayList<ErrMatcher>();
        JSONArray array = bodyRes.getJSONArray("error_matchers");
        for(int i=0;i<array.size();i++) {
            ErrMatcher errMatcher = JSONObject.toJavaObject(array.getJSONObject(i), ErrMatcher.class);
            rsts.add(errMatcher);
        }
        return rsts;
    }
    
    /**
     * 用户打标标签接口 
     * @param userIds 用户userIds，最多传入10条
     * @param label_id 标签id
     * @param label_value 标签值
     * @return List<ErrMatcher> List为空标识全部成功
     * @throws AlipayException 
     */
    public List<ErrMatcher> create(String[] userIds,String label_id,String label_value) throws AlipayException {
        List<Matcher> matchers = new ArrayList<Matcher>();
        for(String userId : userIds) {
            Matcher matcher = new Matcher(userId);
            matchers.add(matcher);
        }
        return create(matchers, label_id,label_value);
    }
    
    
    /**
     * 用户打标标签接口 
     * @param matchers 支付宝用户匹配器列表，最多传入10条
     * @param label_id 标签id
     * @param label_value 标签值
     * @return List<ErrMatcher>
     * @throws AlipayException 
     */
    public List<ErrMatcher> create(List<Matcher> matchers,String label_id,String label_value) throws AlipayException {
        if (ObjectHelper.isEmpty(label_id)) {
            throw new IllegalArgumentException("label_id can't be null or empty");
        }
        if (ObjectHelper.isEmpty(label_value)) {
            throw new IllegalArgumentException("label_value can't be null or empty");
        }
        if (ObjectHelper.isEmpty(matchers)) {
            throw new IllegalArgumentException("matchers can't be null or empty");
        }
        if(matchers.size()>10) {
            throw new IllegalArgumentException("matchers 最多传入10条");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("label_id", label_id);
        body.put("label_value", label_value);
        body.put("matchers", JSONObject.toJSONString(matchers));
        body.put("method", "alipay.open.public.matchuser.label.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_matchuser_label_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_matchuser_label_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<ErrMatcher> rsts = new ArrayList<ErrMatcher>();
        JSONArray array = bodyRes.getJSONArray("error_matchers");
        for(int i=0;i<array.size();i++) {
            ErrMatcher errMatcher = JSONObject.toJavaObject(array.getJSONObject(i), ErrMatcher.class);
            rsts.add(errMatcher);
        }
        return rsts;
    }
}
